package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceimpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateByPrimaryKey(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> list = permissionMapper.queryForList(qo);
        return new PageInfo<>(list);

    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {
        //在数据库中获得所有的权限表达式
        List<String> allExpression = permissionMapper.selectAllExpression();

        //从容器里面获得贴了Controller注解的bean
        //getBeansWithAnnotation
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);

        //获得所有的Controller
        Collection<Object> controllers = beans.values();
        //获取每个controller的方法
        for (Object controller : controllers) {
            //使用反射
            //获得字节码对象
            Class<?> clazz = controller.getClass().getSuperclass();
            //再获得里面的方法
            Method[] methods = clazz.getDeclaredMethods();
            //遍历方法获得贴有自定义注解的方法
            for (Method method : methods) {
                //判断方法有没有贴自己定义的注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                //如果有贴注解,则进行处理(进行权限表达式拼接,跟数据库比较)
                if (annotation!=null) {
                    String expression = annotation.value()[0];
                    //然后进行判断,跟数据库的是否有重复
                    if (!allExpression.contains(expression)) {
                        //如果没有重复的,把数据放到类中,保存到数据库
                        Permission permission = new Permission(null, annotation.value()[1], expression);
                        permissionMapper.insert(permission);
                    }

                }
            }
        }
    }

    @Override
    public List<String> selectByEmpid(Long id) {
        List<String> stringList = permissionMapper.selectByEmpid(id);
        return stringList;
    }
}
